// pkg_index/pages/hy_product/hy_product.js
const app = getApp();
import request from '../../../utils/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isable:false,//是否允许选择产品
    dianPuId:1,
    personId:0,
    shareble:false,
    zhongXinProductId:0,

    userName: '', //联系人
    provinceName: '', //省
    cityName: '',
    countyName: '', //区
    nationalCode: '',
    postalCode: '',
    detailInfo: '', //详细地址
    telNumber: '', //联系电话
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    var that=this

    that.setData({
    
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
    
      personId: wx.getStorageSync(app.globalData.PERSON_ID),


      userName: wx.getStorageSync(app.globalData.USERNAME), //联系人
      provinceName: wx.getStorageSync(app.globalData.PROVINCENAME), //省
      cityName: wx.getStorageSync(app.globalData.CITYNAME),
      countyName: wx.getStorageSync(app.globalData.COUNTYNAME), //区
      nationalCode: wx.getStorageSync(app.globalData.NATIONALCODE),
      postalCode: wx.getStorageSync(app.globalData.POSTALCODE),
      detailInfo: wx.getStorageSync(app.globalData.DETAILINFO), //详细地址
      telNumber: wx.getStorageSync(app.globalData.TELNUMBER), //联系电话
    })

    that.getProductList();
  },


  getProductList() {
    var that = this
    let data = {
 
      dianPuId: that.data.dianPuId,
      personId:that.data.personId,
    }
    request.get('product/getJianKangHuiChanPinList', data, res => {

      wx.hideLoading()
      console.log("权益产品:" + JSON.stringify(res.data))
      that.setData({
        productList: res.data.list,
        isable:res.data.xuanJianKanChanPin==1?false:true
      })

    })
  },


  //选择会员
  radioChange(e) {
    // console.log(e.detail.value)
    this.setData({
      zhongXinProductId: e.detail.value,
      shareble:true
    })

  },


  
  // 关闭分享弹框
  colseShare: function () {
    this.setData({
      shareble: false,
        
    })
  },


    //选择地址
    setAderssTap: function () {
      var that = this
      if (wx.chooseAddress) {
        wx.chooseAddress({
          success: function (res) {
            wx.setStorageSync(app.globalData.USERNAME, res.userName);
            wx.setStorageSync(app.globalData.PROVINCENAME, res.provinceName);
            wx.setStorageSync(app.globalData.CITYNAME, res.cityName);
            wx.setStorageSync(app.globalData.COUNTYNAME, res.countyName);
            wx.setStorageSync(app.globalData.DETAILINFO, res.detailInfo);
            wx.setStorageSync(app.globalData.TELNUMBER, res.telNumber);
            wx.setStorageSync(app.globalData.NATIONALCODE, res.nationalCode);
            wx.setStorageSync(app.globalData.POSTALCODE, res.postalCode);
            that.setData({
              userName: res.userName,
              provinceName: res.provinceName,
              cityName: res.cityName,
              countyName: res.countyName,
              detailInfo: res.detailInfo,
              telNumber: res.telNumber,
              nationalCode: res.nationalCode,
              postalCode: res.postalCode,
            })
        
          },
          fail: function () {
  
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.address"]) { ////如果用户重新同意了授权
                  wx.chooseAddress({
                    success: function (ress) {
                      wx.setStorageSync(app.globalData.USERNAME, ress.userName);
                      wx.setStorageSync(app.globalData.PROVINCENAME, ress.provinceName);
                      wx.setStorageSync(app.globalData.CITYNAME, ress.cityName);
                      wx.setStorageSync(app.globalData.COUNTYNAME, ress.countyName);
                      wx.setStorageSync(app.globalData.DETAILINFO, ress.detailInfo);
                      wx.setStorageSync(app.globalData.TELNUMBER, ress.telNumber);
                      wx.setStorageSync(app.globalData.NATIONALCODE, ress.nationalCode);
                      wx.setStorageSync(app.globalData.POSTALCODE, ress.postalCode);
                      that.setData({
                        userName: ress.userName,
                        provinceName: ress.provinceName,
                        cityName: ress.cityName,
                        countyName: ress.countyName,
                        detailInfo: ress.detailInfo,
                        telNumber: ress.telNumber,
                        nationalCode: ress.nationalCode,
                        postalCode: ress.postalCode,
                      })
                    
                    },
                  })
                }
              
              }
            })
          }
        })
      } else {
        console.log('当前微信版本不支持chooseAddress');
      }
  
      // wx.chooseInvoiceTitle({
      //     success(res) {
      //          console.log("发票:"+JSON.stringify(res))
      //     }
      // })
    },
  

    isAction() {
      var that=this
      if (that.data.zhongXinProductId>0 && that.data.wxTelNumber!='') {
       
        wx.showLoading({
          title: '处理中...',
          mask: true
        })

        let data = {
 
          dianPuId: that.data.dianPuId,
          zhongXinProductId: that.data.zhongXinProductId,

          wxUserName: that.data.userName,
          wxPostalCode: that.data.postalCode,
          wxProvinceName: that.data.provinceName,
          wxCityName: that.data.cityName,
          wxCountyName: that.data.countyName,
          wxDetailInfo: that.data.detailInfo,
          wxNationalCode: that.data.nationalCode,
          wxTelNumber: that.data.telNumber,
        }

        request.get('OrderForm20231109/jianKangQuanHuiYuanXuanChanPin', data, res => {

          wx.hideLoading()
          // console.log("注册:" + JSON.stringify(res.data))
          if (res.data.status == 1) {
           
            wx.showModal({
                  title: '订单提交成功',
                  icon: "none",
                  content: "您可以在'我'查看订单发货情况",
                  duration: 1000,
                  showCancel:false,
                  mask: false,
                  success (res) {
                    if (res.confirm) {
                      wx.navigateBack({
                        delta: 1,
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })

          } else { //订单生成失败

            wx.showToast({
              title: "订单生成失败！", // 提示的内容
              icon: "none", // 图标，默认success
              duration: 3000, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
              complete: function () {
                that.setData({
                  isClick: false
                })
              }
            })
          }

        })
      } else {
        wx.showToast({
          title: "请选择产品，以及收货地址！", // 提示的内容
          icon: "none", // 图标，默认success
          duration: 3000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
          complete: function () {

          }
        })
      }

    },
    
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})